#!/usr/bin/env python
# coding=utf-8

if __name__ == '__main__':
    """ for file debug"""
    import sys,os
    sys.path.insert(0,os.path.join( os.getcwd(), '..' ))

from tracker import trackData
from tracker.track_gripper import TrackGripper
from vavava.loop_timer import LoopTimer


__author__ = 'vavava'


class TrackerDispatcher(LoopTimer):
    """"""
    def __init__(self,debug_level=0,period=1000,soc_timeout=10,log=None,itemIds=[],
                 host="localhost",port=3306,user="root",passwd="root",db="wr_trace"):
        LoopTimer.__init__(self,function=self.dispatch,interval=period,log=log)
        self.grippers={}
        dbo = trackData.DBObject(host,port,user,passwd,db,log)
        self.db= trackData.Tracking(dbObj=dbo)
        for itemId in itemIds:
            self.grippers[itemId]=TrackGripper(dbObj=self.db,log=log,itemId=itemId,
                debug_level=debug_level,soc_timeout=soc_timeout)

    def dispatch(self):
        self.log.debug("dispatch begin")
        for gripper in self.grippers:
            if self.TimerIsRunning:
                self.grippers[gripper].track()
            else:
                break
        self.log.debug("dispatch end")


